package server.handler.friendHandler;

import com.alibaba.fastjson.JSONObject;
import common.EnvelopeMessage;
import common.messages.HistoryMessage;
import server.dao.MessageDao;
import server.handler.SimpleHandler;
import server.session.Session;

import java.util.ArrayList;

public class PrivateHistoryHandler implements SimpleHandler {
    @Override
    public void handle(EnvelopeMessage msg, Session session) {
        JSONObject body = msg.getBody();
        Integer userId = body.getInteger("userId");
        String friendName = body.getString("friendName");
        Integer friendId = body.getInteger("friendId");
        ArrayList<HistoryMessage> historyMessages = (ArrayList<HistoryMessage>) new MessageDao().getPrivateMessages(userId,friendId);
        JSONObject object = new JSONObject();
        object.put("friendName",friendName);
        object.put("messages",historyMessages);
        EnvelopeMessage back = new EnvelopeMessage("history","private",object);
        String json = JSONObject.toJSONString(back);
        System.out.println("服务端返回"+json);
        session.send(json);

    }
}
